package internal

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"

	log "github.com/sirupsen/logrus"
)

// 体力加速活动
type EnergyQuick struct {
	activID  int64 // 活动ID
	sTime    int64 // 活动开始时间
	eTime    int64 // 活动结束时间
	multiple int64 // 倍率
	// rw            sync.RWMutex
}

func NewEnergyQuick() *EnergyQuick {
	return &EnergyQuick{}
}

// 启动服务器，入口
func (this *EnergyQuick) Init() {
	this.CheckRestoreEnergy()
}

func (this *EnergyQuick) Run(closeSig chan bool) {

}

func (this *EnergyQuick) Destroy() {

}

// 获取恢复的体力值
func (this *EnergyQuick) GetRestoreEnergy() (r int64) {
	r = 5
	nowUnix := utils.TNow().Unix()
	if this.sTime <= nowUnix && nowUnix <= this.eTime && this.multiple > 1 {
		r *= this.multiple
	}
	return
}

// 检测活动
func (this *EnergyQuick) CheckRestoreEnergy() {
	nowUnix := utils.TNow().Unix()
	tplLst, ok := data.Filters("ActActivities", func(val interface{}) bool {
		valT := val.(data.ActActivity)
		actTyp := int64(valT.ActTyp)
		beginTime, _ := utils.LayoutToTime(valT.BeginTime)
		endTime, _ := utils.LayoutToTime(valT.EndTime)
		// 体力加速活动 活动已经开始
		return actTyp == data.ActivityEnum.EnergyQuick && beginTime.Unix() <= nowUnix && nowUnix <= endTime.Unix()
	})

	if !ok || len(tplLst) == 0 {
		log.WithFields(log.Fields{
			"method": "CheckRestoreEnergy",
		}).Debug("没有体力加速活动")
		return
	}
	activ := tplLst[0].(data.ActActivity)

	tpl, ok := data.Get("EnergyActivitys", activ.ActivId)
	if !ok {
		log.WithFields(log.Fields{
			"method":  "CheckRestoreEnergy",
			"activId": activ.ActivId,
		}).Debug("体力加速活动配表EnergyActivitys，没有数据。")
		return
	}
	ea := tpl.(data.EnergyActivity)

	sTime, _ := utils.LayoutToTime(activ.BeginTime)
	eTime, _ := utils.LayoutToTime(activ.EndTime)

	this.activID = int64(activ.ActivId)
	this.sTime = sTime.Unix()
	this.eTime = eTime.Unix()
	this.multiple = int64(ea.Multiple)

	log.WithFields(log.Fields{
		"method":   "CheckRestoreEnergy",
		"activId":  this.activID,
		"sTime":    this.sTime,
		"eTime":    this.eTime,
		"multiple": this.multiple,
	}).Info("体力加速活动开启")
	return
}
